package au.com.iglootech.TaxTimeToolTip.ui.company;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import au.com.iglootech.TaxTimeToolTip.Entity.Company;
import au.com.iglootech.TaxTimeToolTip.Entity.FeeRate;
import au.com.iglootech.TaxTimeToolTip.Exception.AppX;
import au.com.iglootech.TaxTimeToolTip.service.CompanyService;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;

@Configurable(preConstruction=true)
@Component
@Scope("request")
public class CreateCompanyTab extends CustomComponent {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private transient CompanyService companyService;
	
	VerticalLayout mainLayout = new VerticalLayout();
	private Company newCompany;
	private BeanItem<Company> personItem;

	public CreateCompanyTab() {
		setCaption("Add Company");
		newCompany = new Company();
		initContent();
	}

	private void initContent() {
		Panel panel = new Panel();
		final Form companyForm = new Form();

		personItem = new BeanItem<Company>(newCompany);
		companyForm.setCaption("Company details");
		companyForm.setWriteThrough(false); // we want explicit 'apply'
		companyForm.setInvalidCommitted(false); // no invalid values in
												// datamodel

		// FieldFactory for customizing the fields and adding validators
		companyForm.setFormFieldFactory(new CompanyFieldsFactory());
		companyForm.setItemDataSource(personItem); // bind to POJO via BeanItem

		// Determines which properties are shown, and in which order:
		companyForm.setVisibleItemProperties(Arrays.asList(new String[] {
				"companyName", "contactName" ,"contactNumber", "tfn", "email", "address1", "address2", "address3" }));
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);

		Button apply = new Button("Apply", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				try {
					companyForm.commit();
					companyService.createCompany(newCompany);
					getWindow().showNotification("You company has been saved.");
					newCompany = new Company();
					companyForm.discard();
					personItem = new BeanItem<Company>(newCompany);;
					companyForm.setItemDataSource(personItem);
					companyForm.setVisibleItemProperties(Arrays.asList(new String[] {
							"companyName", "contactName" ,"contactNumber", "tfn", "email", "address1", "address2", "address3" }));
				} catch (Exception e) {
					throw new AppX(e.getMessage());
				}
			}
		});
		buttons.addComponent(apply);
		companyForm.getFooter().addComponent(buttons);
		companyForm.getFooter().setMargin(false, false, true, true);
		panel.addComponent(companyForm);
		mainLayout.addComponent(panel);
		this.setCompositionRoot(mainLayout);
	}
}
